<HTML>
<HEAD>
<TITLE>partial_sort()</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Rogue Wave Standard Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="pair.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="partial-sort-copy.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Rogue Wave C++ Standard Library Reference Guide</B></DIV>
<H2>partial_sort()</H2>
<P><B>Module:</B>&nbsp;&nbsp;Standard C++ Library&nbsp;&nbsp;&nbsp;<B>Library:</B>&nbsp;&nbsp;<A HREF="2-9.html">Algorithms</A></P>

<PRE><HR><B><I>Function</I></B><HR></PRE>

<UL>
<LI><A HREF="#sec1">Local Index</A></LI>
<LI><A HREF="#sec2">Summary</A></LI>
<LI><A HREF="#sec3">Synopsis</A></LI>
<LI><A HREF="#sec4">Description</A></LI>
<LI><A HREF="#sec5">Complexity</A></LI>
<LI><A HREF="#sec6">Example</A></LI>
<LI><A HREF="#sec7">See Also</A></LI>
<LI><A HREF="#sec8">Standards Conformance</A></LI>
</UL>
<A NAME="sec1"><H3>Local Index</H3></A>
No Entries
<A NAME="sec2"><H3>Summary</H3></A>
<P>A generic algorithm for sorting collections of entities</P>
<A NAME="sec3"><H3>Synopsis</H3></A>

<PRE>#include &lt;algorithm&gt;

namespace std {
  template &lt;class RandomAccessIterator&gt;
  void partial_sort(RandomAccessIterator start,
                    RandomAccessIterator mid,
                    RandomAccessIterator finish);

  template &lt;class RandomAccessIterator, class Compare&gt;
  void partial_sort(RandomAccessIterator start,
                    RandomAccessIterator mid, 
                    RandomAccessIterator finish, 
                    Compare comp);
}
</PRE>
<A NAME="sec4"><H3>Description</H3></A>
<P>The <SAMP>partial_sort()</SAMP> algorithm takes the range <SAMP>[start,finish)</SAMP> and sorts the first <SAMP>mid - start</SAMP> values in ascending order. The remaining elements in the range (those in <SAMP>[mid, finish)</SAMP>) are not in any defined order. The first version of the algorithm uses <SAMP>operator&lt;()</SAMP> as the comparison operator for the sort. The second version uses the function object <SAMP>comp</SAMP>.</P>
<A NAME="sec5"><H3>Complexity</H3></A>
<P><SAMP>partial_sort()</SAMP> does approximately<SAMP> (finish  - start) * log(mid-start) </SAMP>comparisons. The Rogue Wave implementation of <SAMP>partial_sort()</SAMP> is implemented using heapsort, which is <SAMP>O(N * log(N))</SAMP> in the worst case. </P>
<A NAME="sec6"><H3>Example</H3></A>

<UL><PRE>//
//  partsort.cpp
//

#include &lt;algorithm&gt;   // for partial_sort
#include &lt;iostream&gt;    // for cout, endl
#include &lt;iterator&gt;    // for ostream_iterator
#include &lt;vector&gt;      // for vector



int main()
{
    typedef std::vector&lt;int, std::allocator&lt;int&gt; &gt; Vector;

    typedef std::ostream_iterator&lt;int, char, 
                                  std::char_traits&lt;char&gt; &gt;
            Iter;

    const Vector::value_type a[] = {
        17, 3,  5,  -4, 1, 12, -10, -1, 14, 7,
        -6, 8, 15, -11, 2, -2,  18,  4, -3, 0
    };

    Vector v1 (a + 0, a + sizeof a / sizeof *a);

    // Output original vector.
    std::cout &lt;&lt; "For the vector: ";
    std::copy (v1.begin (), v1.end (), Iter (std::cout, " "));

    // Partial sort the first seven elements.
    std::partial_sort (v1.begin (), v1.begin () + 7, 
                       v1.end ());

    // Output result.
    std::cout &lt;&lt; "\n\nA partial_sort of seven elements "
              &lt;&lt; "gives: \n     ";
    std::copy (v1.begin (), v1.end (), Iter (std::cout, " "));
    std::cout &lt;&lt; std::endl;

    // A vector of ten elements.
    Vector v2 (Vector::size_type (10), 0);

    // Sort the last ten elements in v1 into v2.
    std::partial_sort_copy (v1.begin () + 10, v1.end (),
                            v2.begin (), v2.end ());

    // Output result.
    std::cout
        &lt;&lt; "\nA partial_sort_copy of the last ten elements "
        &lt;&lt; "gives: \n     ";
    std::copy (v2.begin (), v2.end (), Iter (std::cout, " "));

    std::cout &lt;&lt; std::endl;

    return 0;
}


Program Output:
</PRE></UL>
<UL><PRE>For the vector: 17 3 5 -4 1 12 -10 -1 14 7 -6 8 15 -11 2 -2 
18 4 -3 0 

A partial_sort of seven elements gives: 
     -11 -10 -6 -4 -3 -2 -1 17 14 12 7 8 15 5 3 2 18 4 1 0 

A partial_sort_copy of the last ten elements gives: 
     0 1 2 3 4 5 7 8 15 18 
</PRE></UL>
<A NAME="sec7"><H3>See Also</H3></A>
<P><SAMP><A HREF="sort.html">sort()</A></SAMP>, <SAMP><A HREF="stable-sort.html">stable_sort()</A></SAMP>, <SAMP><A HREF="partial-sort-copy.html">partial_sort_copy()</A></SAMP> </P>
<A NAME="sec8"><H3>Standards Conformance</H3></A>
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.3.1.3</I></P>

<BR>
<HR>
<A HREF="pair.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="partial-sort-copy.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A></BODY>
</HTML>
